<?php

namespace App\Service;

use App\Model\UserFollowModel;
use App\Model\StaffModel;
use App\Utility\DataConversion;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

class FriendService extends BaseService
{
    /**
     * @param $sid
     * @param $type
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMyFriend($sid, $type): array
    {
        if ($type == 0) {
            $friends = UserFollowModel::create()->where(['sid' => $sid, 'is_deleted' => 0])->all();
            $fids = array_column($friends, 'fsid');
        } else {
            $friends = UserFollowModel::create()->where(['fsid' => $sid, 'is_deleted' => 0])->all();
            $fids = array_column($friends, 'sid');
        }
        if (empty($friends)) {
            return [
                'result' => false,
                'msg' => '暂无好友'
            ];
        }
        $users = StaffModel::create()->where("id", $fids, "IN")->all();

        $list = [];
        //$data = [];
        $list1 = [];
        foreach ($friends as $friend) {
            foreach ($users as $user) {
                if ($type == 0 && $friend['fsid'] == $user['id']) {
                    $nickname = !empty($friend['memo']) ? $friend['memo'] : $user['nickname'];
                    $asc = DataConversion::getInstance()->getFirstCharter($nickname) ?: "#";
                    if ($asc != '#' && !isset($list[$asc])) {
                        $list[$asc] = [
                            'letter' => $asc,
                            'data' => []
                        ];
                    }

                    $res = [
                        'id' => $user['id'],
                        'sid' => $user['id'],
                        'nickname' => $nickname,
                        'userid' => $user['userid'],
                        'avatar' => $user['avatar'],
                        'sign' => $user['dialogue'] ?? '这个人很懒，什么都没有留下',
                        'mutual' => $this->getMutual($sid, $friend['fsid'])
                    ];
                    if ($asc == '#') {
                        $list1[] = $res;
                    } else {
                        $list[$asc]['data'][] = $res;
                    }
                    break;
                } elseif ($type == 1 && $friend['sid'] == $user['id']) {
                    $asc = DataConversion::getInstance()->getFirstCharter($user['nickname']) ?: "#";
                    if ($asc != '#' && !isset($list[$asc])) {
                        $list[$asc] = [
                            'letter' => $asc,
                            'data' => []
                        ];
                    }

                    $res = [
                        'id' => $user['id'],
                        'sid' => $user['id'],
                        'nickname' => $user['nickname'],
                        'userid' => $user['userid'],
                        'avatar' => $user['avatar'],
                        'sign' => $user['dialogue'] ?? '这个人很懒，什么都没有留下',
                        'mutual' => $this->getMutual($friend['sid'], $sid)
                    ];
                    if ($asc == '#') {
                        $list1[] = $res;
                    } else {
                        $list[$asc]['data'][] = $res;
                    }
                    break;
                }
            }
        }

        array_multisort(array_column($list, 'letter'), SORT_ASC, SORT_NUMERIC, $list);
        if (!empty($list1)) {

            $list[] = [
                'letter' => "#",
                'data' => $list1
            ];
        }

        return [
            'result' => true,
            'msg' => '获取成功',
            'data' => array_values($list)
        ];
    }

    /**
     * @param $sid
     * @param $fsid
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMutual($sid, $fsid): bool
    {
        $follow = UserFollowModel::create()->getUser(['fsid' => $sid, 'sid' => $fsid, 'is_deleted' => 0]);
        if (!$follow) return false;
        return true;
    }

    /**
     * @param $sid
     * @param $fsid
     * @param $memo
     * @return array
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function changeMemo($sid, $fsid, $memo): array
    {
        $follow = UserFollowModel::create()->getUser(['sid' => $sid, 'fsid' => $fsid, 'is_deleted' => 0]);
        if (!$follow) {
            return [
                'result' => false,
                'msg' => '暂未关注该用户'
            ];
        }

        $par = [
            'memo' => $memo
        ];

        $res = UserFollowModel::create()->update($par, ['sid' => $sid, 'fsid' => $fsid, 'is_deleted' => 0]);
        if (!$res) {
            return [
                'result' => false,
                'msg' => '操作失败'
            ];
        }
        return [
            'result' => true,
            'msg' => '操作成功',
            'data' => 'success'
        ];
    }

    /**
     * @param $sid
     * @param $fsid
     * @return mixed|string|null
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function getMemo($sid, $fsid)
    {
        $follow = UserFollowModel::create()->getUser(['sid' => $sid, 'fsid' => $fsid, 'is_deleted' => 0]);
        if (!$follow) return '';
        return $follow->memo;
    }
}